package com.nsoft.ui;

import com.nsoft.ui.BookListFragment.OnBookSelectListener;
import com.nsoft.utils.Constant;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.ImageButton;

public class BookListActivity extends FragmentActivity 
				implements OnBookSelectListener {
	boolean isTempViewVisible=false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_view);
		
		if (findViewById(R.id.fragment_container)!=null){
			if (savedInstanceState!=null)
				return;
		
			BookListFragment bookListFrag=
							new BookListFragment();
			bookListFrag.setArguments(getIntent().getExtras());
			getSupportFragmentManager().beginTransaction().
				add(R.id.fragment_container,bookListFrag).
				commit();
			
			startUp();
		}
		
	}

	private void startUp(){
		
		ImageButton settingBtn=(ImageButton)
				findViewById(R.id.settingButton);
		settingBtn.setOnClickListener(new 
			View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent=new Intent(
							BookListActivity.this,
							SettingActivity.class);
					BookListActivity.this.
						startActivity(intent);
				}
			});
		
    	ImageButton exitBtn=(ImageButton)
    			findViewById(R.id.exitButton);
    	exitBtn.setOnClickListener(new 
    				View.OnClickListener() {
			@Override
			public void onClick(View v) {
				AlertDialog.Builder alert=
					new AlertDialog.Builder(
							BookListActivity.this);
				alert.setIcon(R.drawable.ic_help);
				alert.setTitle(Constant.DIALOG_CONFIRM).
					setMessage(Constant.
							booklist.BLA_CFM_QUIT).
					setPositiveButton(Constant.DIALOG_YES,
						new DialogInterface.OnClickListener(){
							@Override
							public void onClick(
									DialogInterface arg0, 
									int arg1){
								finish();
							}
						
					}).setNegativeButton(Constant.DIALOG_NO,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(
									DialogInterface dialog,
									int which){
								dialog.cancel();
							}
					}).create().show();
						
					
			}
		});
	}
	
	@Override
	public void onBookSelected(int idx) {
		/*
		BookTempViewFragment bookTempViewFrag=
			(BookTempViewFragment)getSupportFragmentManager().
			findFragmentById(R.id.booktempview_fragment);
		
		if (bookTempViewFrag!=null){
			bookTempViewFrag.bookSelected(idx);
		}else{
			Toast.makeText(this,
					"list index: "+idx+" is pressed",
					Toast.LENGTH_SHORT).
					show();
		}
		*/
	}
	
	@Override
	protected void onDestroy() {
		finish();
		super.onDestroy();
	}
}
